package com.huifaapp.mall.base.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.free.statuslayout.manager.OnRetryListener;
import com.free.statuslayout.manager.OnShowHideViewListener;
import com.free.statuslayout.manager.StatusLayoutManager;
import com.huifaapp.mall.R;
import com.huifaapp.mall.base.basemvp.IPresenter;
import com.huifaapp.mall.base.basemvp.IView;
import com.jaeger.library.StatusBarUtil;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * import static android.R.attr.author;
 * import static android.R.attr.description;
 *
 * @author: wangZ
 * @date：2017/6/30 10:54
 * FileName：BaseActivity
 * @description：实现一下IView接口中简单的逻辑几初始化操作 <p>
 * 注意:View层和Presenter层的关联和取笑关联已经在本类里实现,子类无需再实现
 * <p>
 * <p>
 * <p>
 * <p>
 */

public abstract class BaseActivity<P extends IPresenter> extends FragmentActivity implements IView {

    private P mPresenter;
    private Unbinder unbinder;
    private StatusLayoutManager statusLayoutManager;
    public Activity mContext;
    public  String TAG;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TAG= this.getClass().getName().toString();
        mContext = this;
        initSystemBarTint();
        Bundle extras = getIntent().getExtras();

        if (getLayoutId() == 0) {
            throw new NullPointerException("getLayoutId方法,没有返回布局ID");

        } else {
            setContentView(getLayoutId());
            unbinder = ButterKnife.bind(this);
            initViews(savedInstanceState);
        }
        mPresenter = onLoadPresenter();
        //关联View层和Presenter层
        if (mPresenter != null) {
            mPresenter.attachView(this, this);
        }
        initTitleBar();
        initData(extras);
    }

    @Override
    protected void onDestroy() {
        //解除对Presenter的关联
        if (mPresenter != null) {
            mPresenter.detachView();
        }
        super.onDestroy();
        if (unbinder != null) {
            unbinder.unbind();
        }

    }

    protected  void   initTitleBar(){

    };

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        View view = getLayoutInflater().inflate(R.layout.activity_base, null);
        //设置填充activity_base布局
        super.setContentView(view);
        //加载子类Activity的布局
        initDefaultView(layoutResID);
    }

    /**
     * 初始化默认布局的View
     *
     * @param layoutResId 子View的布局id
     */
    private void initDefaultView(int layoutResId) {
        //初始化界面多状态加载View
        statusLayoutManager = StatusLayoutManager.newBuilder(this)
                .contentView(layoutResId)
                .emptyDataView(R.layout.layout_emptydata)
                .errorView(R.layout.layout_error)
                .loadingView(R.layout.layout_loading)
                .netWorkErrorView(R.layout.layout_networkerror)
                .retryViewId(R.id.button_retry)
                .onShowHideViewListener(new OnShowHideViewListener() {
                    @Override
                    public void onShowView(View view, int id) {
                    }

                    @Override
                    public void onHideView(View view, int id) {
                    }
                }).onRetryListener(new OnRetryListener() {
                    @Override
                    public void onRetry() {
                    }
                }).build();

        FrameLayout container = (FrameLayout) findViewById(R.id.fl_activity_child_container);
//        View childView = LayoutInflater.from(this).inflate(layoutResId, null);
        container.addView(statusLayoutManager.getRootLayout(), 0);
        if (statusLayoutManager != null) {
            //显示内容
            statusLayoutManager.showContent();
        }


    }

    /**
     * 设置状态栏颜色
     */
    private void initSystemBarTint() {
        Window window = getWindow();
        if (translucentStatusBar()) {
            // 设置状态栏全透明
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(Color.TRANSPARENT);
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            }
            return;
        }
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //5.0以上使用原生方法
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//            window.setStatusBarColor(setStatusBarColor());
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //4.4-5.0使用三方工具类
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//            SystemBarTintManager tintManager = new SystemBarTintManager(this);
//            tintManager.setStatusBarTintEnabled(true);
//            tintManager.setStatusBarTintColor(setStatusBarColor());
//            StatusBarUtil.setColor(this, setStatusBarColor());
        }
    }

    /**
     * 子类可以重写决定是否使用透明状态栏
     *
     * @return 返回true为状态栏全透明，可以沉浸全屏图片，默认为false
     * 全屏沉浸图片时，需要把xml里的android:fitsSystemWindows="true"去掉
     */
    protected boolean translucentStatusBar() {
        return false;
    }

    /**
     * 子类可以重写改变状态栏颜色
     *
     * @return 主题色，即标题栏颜色
     */
//    protected int setStatusBarColor() {
//        int color = getResources().getColor(R.color.white);
//        return color;
//    }

    @Override
    public void showLoading() {
        if (statusLayoutManager != null) {
            statusLayoutManager.showLoading();
        }

    }

    @Override
    public void showContent() {
        if (statusLayoutManager != null) {
            statusLayoutManager.showContent();
        }
    }

    @Override
    public void showEmptyData() {
        if (statusLayoutManager != null) {
            statusLayoutManager.showEmptyData();
        }
    }

    @Override
    public void showError() {
        if (statusLayoutManager != null) {
            statusLayoutManager.showError();
        }
    }

    @Override
    public void showNetWorkError() {
        statusLayoutManager.showNetWorkError();
    }

    /**
     * 获取Presenter
     *
     * @return Presenter
     */
    public P getPresenter() {
        return mPresenter;
    }

    protected abstract void initData(Bundle extras);

    protected  void initViews(Bundle savedInstanceState){};

    /**
     * 加载Presenter
     *
     * @return
     */
    protected abstract P onLoadPresenter();

    /**
     * 获取布局ID
     *
     * @return 布局id
     */
    protected abstract int getLayoutId();


    /**
     *
     */

    /**
     * 打开一个Activity 默认 不关闭当前activity
     *
     * @param clz 目标activity
     */
    public void gotoActivity(Class<?> clz) {
        gotoActivity(clz, false, null);
    }

    /**
     * 打开一个Activity
     *
     * @param clz                    目标activity
     * @param isCloseCurrentActivity 是关闭当前activity
     */
    public void gotoActivity(Class<?> clz, boolean isCloseCurrentActivity) {
        gotoActivity(clz, isCloseCurrentActivity, null);
    }

    /**
     * 打开一个Activity 默
     *
     * @param clz                    目标activity
     * @param isCloseCurrentActivity 是关闭当前activity
     * @param ex                     需要传递的数据
     */
    public void gotoActivity(Class<?> clz, boolean isCloseCurrentActivity, Bundle ex) {
        Intent intent = new Intent(this, clz);
        if (ex != null) intent.putExtras(ex);
        startActivity(intent);
        if (isCloseCurrentActivity) {
            finish();
        }
    }

}